package my.ts.code.parser;

import java.io.CharArrayWriter;
import java.util.ArrayList;

import my.ts.code.objects.Item;
import my.ts.code.objects.Root;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Parser extends DefaultHandler {
	// 06-02 02:20:58.759: I/System.out(1123): STR=<?xml version="1.0"
	// encoding="UTF-8" standalone="no"?><root><message>Tài khoản của bạn đã hết
	// thời gian sử dụng dịch vụ, Xin vui lòng nạp tài khoản để được sử dụng 15
	// ngày tiếp theo.</message></root>

	private String ROOT = "root";
	private String more = "more";
	private String message = "message";

	private String ITEM = "item";
	private String Item = "Item";
	private String ITEMS = "items";
	private String Items = "Items";
	private String CategoryID = "CategoryID";
	private String CategoryName = "CategoryName";
	private String Content = "Content";
	private String ImageURL = "ImageURL";
	private String HeaderName = "HeaderName";
	private String Source = "Source";

	private String HeaderID = "HeaderID";
	private String EventID = "EventID";
	private String EventName = "EventName";
	private String Summary = "Summary";
	private String LastUpdate = "LastUpdate";

	private String StoryCategoryID = "StoryCategoryID";
	private String StoryCategoryName = "StoryCategoryName";

	private String Title = "Title";
	private String AccountID = "AccountID";

	// Image
	private String ImageID = "ImageID";
	private String ImageName = "ImageName";
	private String URLSize1 = "URLSize1";
	private String URLSize2 = "URLSize2";
	private String URLSize3 = "URLSize3";

	// Game
	private String GameAlbumID = "GameAlbumID";
	private String GameAlbumName = "GameAlbumName";
	private String GameID = "GameID";
	private String GameName = "GameName";
	private String URLThumb = "URLThumb";
	private String SMSDownloadCode = "SMSDownloadCode";
	private String URLImage = "URLImage";
	private String Description = "Description";

	// Lottery
	private String ProvinceID = "ProvinceID";
	private String ProvinceName = "ProvinceName";

	private String ProvinceCode = "ProvinceCode";
	private String OpenDate = "OpenDate";

	private String Special_0 = "Special_0";
	private String Special_1 = "Special_1";

	private String First = "First";
	private String Second = "Second";

	private String Third = "Third";
	private String Fourth = "Fourth";

	private String Fifth = "Fifth";
	private String Seventh = "Seventh";

	private String Eighth = "Eighth";

	//

	private String ImageCategoryID = "ImageCategoryID";
	private String ImageCategoryName = "ImageCategoryName";

	private String ImageAlbumID = "ImageAlbumID";
	private String ImageAlbumName = "ImageAlbumName";

	// Video
	private String VideoAlbumID = "VideoAlbumID";
	private String VideoAlbumName = "VideoAlbumName";
	private String VideoID = "VideoID";
	private String VideoName = "VideoName";
	private String URL3GP = "URL3GP";

	// Music
	private String MusicCategoryID = "MusicCategoryID";
	private String MusicCategoryName = "MusicCategoryName";

	private String MusicAlbumID = "MusicAlbumID";
	private String MusicAlbumName = "MusicAlbumName";

	private String MusicID = "MusicID";
	private String MusicName = "MusicName";

	private String ListenTrialURL = "ListenTrialURL";
	private String ListenFullURL = "ListenFullURL";

	// Audio
	private String AudioAlbumID = "AudioAlbumID";
	private String AudioAlbumName = "AudioAlbumName";
	private String AudioID = "AudioID";
	private String AudioName = "AudioName";
	private String URLListen = "URLListen";

	// Postcard
	private String PostCardCategoryID = "PostCardCategoryID";
	private String PostCardCategoryName = "PostCardCategoryName";

	private String PostCardID = "PostCardID";
	private String PostCardName = "PostCardName";

	// private String HeaderName = "HeaderName";
	// private String HeaderName = "HeaderName";

	private boolean done = false, inItem = false;

	/*
	 * inItem: vì trong file xml có thẻ desctipton mà không phải của đối tượng
	 * item, nên dùng biến inItem để biết rằng description là ở trong item(của
	 * item) done: vì parse file xml theo kiểu stream nên có khi bị đứt kết nối
	 * giữa chừng dẫn đến việc chưa có đầy đủ dữ liệu->parse sai. done = true
	 * khi đã bắt dc thẻ cuối cùng của file xml
	 */

	private Root root;
	private Item item;
	private ArrayList<Item> items = new ArrayList<Item>();

	private CharArrayWriter contents = new CharArrayWriter();

	public Root getRoot() {
		if (done)
			return root;
		else
			return null;
	}

	@Override
	public void startDocument() throws SAXException {
	}

	@Override
	public void endDocument() throws SAXException {
	}

	@Override
	public void startElement(String n, String l, String q, Attributes a)
			throws SAXException {
		if (l.equals(ROOT)) {
			inItem = true;
			root = new Root();
		} else if (l.equals(ITEMS) || l.equals(Items)) {
			items = new ArrayList<Item>();
		} else if (l.equals(ITEM) || l.equals(Item)) {
			item = new Item();
		} else if (l.equals(CategoryID) || l.equals(StoryCategoryID)
				|| l.equals(PostCardCategoryID) || l.equals(AudioAlbumID)
				|| l.equals(MusicCategoryID) || l.equals(VideoAlbumID)
				|| l.equals(ImageCategoryID) || l.equals(GameAlbumID)) {
			contents.reset();
		} else if (l.equals(CategoryName) || l.equals(StoryCategoryName)
				|| l.equals(PostCardCategoryName) || l.equals(AudioAlbumName)
				|| l.equals(MusicCategoryName) || l.equals(VideoAlbumName)
				|| l.equals(ImageCategoryName) || l.equals(GameAlbumName)) {
			contents.reset();
		} else if (l.equals(Content)) {
			contents.reset();
		} else if (l.equals(ImageURL) || l.equals(URLImage)) {
			contents.reset();
		} else if (l.equals(HeaderName) || l.equals(ImageAlbumName)
				|| l.equals(PostCardName) || l.equals(ImageName)
				|| l.equals(ProvinceName) || l.equals(AudioName)
				|| l.equals(VideoName) || l.equals(MusicName)
				|| l.equals(GameName) || l.equals(MusicAlbumName)) {
			contents.reset();
		} else if (l.equals(Source)) {
			contents.reset();
		} else if (l.equals(HeaderID) || l.equals(ImageAlbumID)
				|| l.equals(PostCardID) || l.equals(ImageID)
				|| l.equals(ProvinceID) || l.equals(MusicID)
				|| l.equals(AudioID) || l.equals(VideoID) || l.equals(GameID)
				|| l.equals(MusicAlbumID)) {
			contents.reset();
		} else if (l.equals(EventID)) {
			contents.reset();
		} else if (l.equals(EventName)) {
			contents.reset();
		} else if (l.equals(Summary)) {
			contents.reset();
		} else if (l.equals(LastUpdate)) {
			contents.reset();
		} else if (l.equals(URLThumb)) {
			contents.reset();
		} else if (l.equals(SMSDownloadCode)) {
			contents.reset();
		} else if (l.equals(ListenTrialURL) || l.equals(URLListen)) {
			contents.reset();
		} else if (l.equals(ListenFullURL)) {
			contents.reset();
		} else if (l.equals(URL3GP)) {
			contents.reset();
		} else if (l.equals(Title)) {
			contents.reset();
		} else if (l.equals(URLSize1)) {
			contents.reset();
		} else if (l.equals(URLSize2)) {
			contents.reset();
		} else if (l.equals(URLSize3)) {
			contents.reset();
		} else if (l.equals(Description)) {
			contents.reset();
		} else if (l.equals(more)) {
			contents.reset();
		} else if (l.equals(AccountID)) {
			contents.reset();
		} else if (l.equals(OpenDate)) {
			contents.reset();
		} else if (l.equals(Special_0)) {
			contents.reset();
		} else if (l.equals(Special_1)) {
			contents.reset();
		} else if (l.equals(First)) {
			contents.reset();
		} else if (l.equals(Second)) {
			contents.reset();
		} else if (l.equals(Third)) {
			contents.reset();
		} else if (l.equals(Fourth)) {
			contents.reset();
		} else if (l.equals(Fifth)) {
			contents.reset();
		} else if (l.equals(Seventh)) {
			contents.reset();
		} else if (l.equals(Eighth)) {
			contents.reset();
		}

	}

	@Override
	public void endElement(String n, String l, String q) throws SAXException {
		if (l.equals(ROOT)) {

			done = true;

		} else if (l.equals(ITEMS) || l.equals(Items)) {

			root.setItems(items);

		} else if (l.equals(ITEM) || l.equals(Item)) {

			items.add(item);

		} else if (l.equals(CategoryID) || l.equals(StoryCategoryID)
				|| l.equals(PostCardCategoryID) || l.equals(AudioAlbumID)
				|| l.equals(MusicCategoryID) || l.equals(VideoAlbumID)
				|| l.equals(ImageCategoryID) || l.equals(GameAlbumID)) {

			String tmp = contents.toString();
			item.setCategoryID(tmp);

		} else if (l.equals(CategoryName) || l.equals(StoryCategoryName)
				|| l.equals(PostCardCategoryName) || l.equals(AudioAlbumName)
				|| l.equals(MusicCategoryName) || l.equals(VideoAlbumName)
				|| l.equals(ImageCategoryName) || l.equals(GameAlbumName)) {

			String tmp = contents.toString();
			item.setCategoryName(tmp);

		} else if (l.equals(Content)) {

			String tmp = contents.toString();
			item.setContent(tmp);

		} else if (l.equals(ImageURL) || l.equals(URLImage)) {

			String tmp = contents.toString();
			item.setImageURL(tmp);

		} else if (l.equals(HeaderName) || l.equals(ImageAlbumName)
				|| l.equals(PostCardName) || l.equals(ImageName)
				|| l.equals(ProvinceName) || l.equals(AudioName)
				|| l.equals(VideoName) || l.equals(MusicName)
				|| l.equals(GameName) || l.equals(MusicAlbumName)) {

			String tmp = contents.toString();
			item.setHeaderName(tmp);

		} else if (l.equals(Source)) {

			String tmp = contents.toString();
			item.setSource(tmp);

		} else if (l.equals(HeaderID) || l.equals(ImageAlbumID)
				|| l.equals(ImageID) || l.equals(PostCardID)
				|| l.equals(ImageName) || l.equals(MusicID)
				|| l.equals(AudioID) || l.equals(VideoID)
				|| l.equals(ProvinceID) || l.equals(GameID)
				|| l.equals(MusicAlbumID)) {

			String tmp = contents.toString();
			item.setHeaderID(tmp);

		} else if (l.equals(EventID)) {

			String tmp = contents.toString();
			item.setEventID(tmp);

		} else if (l.equals(EventName)) {

			String tmp = contents.toString();
			item.setEventName(tmp);

		} else if (l.equals(Summary)) {

			String tmp = contents.toString();
			item.setSummary(tmp);

		} else if (l.equals(LastUpdate)) {

			String tmp = contents.toString();
			item.setLastUpdate(tmp);

		} else if (l.equals(URLThumb)) {

			String tmp = contents.toString();
			item.setUrlThumb(tmp);

		} else if (l.equals(SMSDownloadCode)) {

			String tmp = contents.toString();
			item.setSMSDownloadCode(tmp);

		} else if (l.equals(ListenTrialURL) || l.equals(URLListen)) {

			String tmp = contents.toString();
			item.setListenTrialURL(tmp);

		} else if (l.equals(ListenFullURL)) {

			String tmp = contents.toString();
			item.setListenFullURL(tmp);

		} else if (l.equals(URL3GP)) {

			String tmp = contents.toString();
			item.setURL3GP(tmp);

		} else if (l.equals(Title)) {

			String tmp = contents.toString();
			item.setTitle(tmp);

		} else if (l.equals(URLSize1)) {

			String tmp = contents.toString();
			item.setUrlSize1(tmp);

		} else if (l.equals(URLSize2)) {

			String tmp = contents.toString();
			item.setUrlSize2(tmp);

		} else if (l.equals(URLSize3)) {

			String tmp = contents.toString();
			item.setUrlSize3(tmp);

		} else if (l.equals(Description)) {

			String tmp = contents.toString();
			item.setDescription(tmp);

		} else if (l.equals(more)) {

			String tmp = contents.toString();
			if (tmp.equals("true"))
				root.setMore(true);

		} else if (l.equals(AccountID)) {

			String tmp = contents.toString();
			item.setAccountID(tmp);
		} else if (l.equals(message)) {

			String tmp = contents.toString();
			root.setMessage(tmp);
		} else if (l.equals(OpenDate)) {

			String tmp = contents.toString();
			item.setOpenDate(tmp);
		} else if (l.equals(Special_1)) {

			String tmp = contents.toString();
			item.setSpecial(tmp);
		} else if (l.equals(First)) {

			String tmp = contents.toString();
			item.setFirst(tmp);

		} else if (l.equals(Second)) {

			String tmp = contents.toString();
			item.setSecond(tmp);

		} else if (l.equals(Third)) {

			String tmp = contents.toString();
			item.setThird(tmp);

		} else if (l.equals(Fourth)) {

			String tmp = contents.toString();
			item.setFourth(tmp);

		} else if (l.equals(Fifth)) {

			String tmp = contents.toString();
			item.setFifth(tmp);

		} else if (l.equals(Special_0)) {

			String tmp = contents.toString();
			item.setSixth(tmp);

		} else if (l.equals(Seventh)) {

			String tmp = contents.toString();
			item.setSeventh(tmp);

		} else if (l.equals(Eighth)) {

			String tmp = contents.toString();
			item.setEighth(tmp);

		}
	}

	@Override
	public void characters(char ch[], int start, int length) {
		contents.write(ch, start, length);
	}

}